<template>
  <div class="student-class">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>我的班级</span>
        </div>
      </template>

      <!-- 班级列表 -->
      <el-table :data="classList" style="width: 100%" border stripe>
        <el-table-column prop="id" label="班级ID" width="80" />
        <el-table-column prop="name" label="班级名称" />
        <el-table-column prop="teacherName" label="任课教师" />
        <el-table-column prop="groupName" label="所在小组" />
        <el-table-column label="操作" width="200" align="center">
          <template #default="scope">
            <el-button
                type="primary"
                size="small"
                @click="viewClassDetails(scope.row)"
            >
              查看详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 班级详情对话框 -->
    <el-dialog
        v-model="dialogVisible"
        :title="currentClass ? `${currentClass.name}详情` : '班级详情'"
        width="60%"
    >
      <div v-if="currentClass">
        <h4>基本信息</h4>
        <el-descriptions :column="2" border>
          <el-descriptions-item label="班级名称">{{ currentClass.name }}</el-descriptions-item>
          <el-descriptions-item label="任课教师">{{ currentClass.teacherName }}</el-descriptions-item>
          <el-descriptions-item label="所在小组">{{ currentClass.groupName }}</el-descriptions-item>

        </el-descriptions>

        <h4 style="margin-top: 20px">班级同学</h4>
        <el-table :data="classmates" style="width: 100%" border stripe>
          <el-table-column prop="stuName" label="姓名" />
          <el-table-column prop="currentGrade" label="年级" />
          <el-table-column prop="groupName" label="所在小组" />
        </el-table>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "StudentClass",
  data() {
    return {
      classList: [],
      currentClass: null,
      classmates: [],
      dialogVisible: false
    }
  },
  created() {
    this.fetchMyClasses()
  },
  methods: {
    // 获取我的班级列表
    async fetchMyClasses() {
      try {
        const res = await this.$axios.get('/student/my-classes', {
          headers: {
            Authorization: localStorage.getItem("token")
          }
        })
        if (res.data.code === 200) {
          this.classList = res.data.data
        }
      } catch (error) {
        console.error('获取班级列表失败:', error)
        this.$message.error('获取班级列表失败')
      }
    },

    // 查看班级详情
    async viewClassDetails(classInfo) {
      this.currentClass = classInfo
      this.dialogVisible = true
      try {
        const res = await this.$axios.get(`/student/class-details/${classInfo.id}`, {
          headers: {
            Authorization: localStorage.getItem("token")
          }
        })
        if (res.data.code === 200) {
          this.classmates = res.data.data
        }
      } catch (error) {
        console.error('获取班级详情失败:', error)
        this.$message.error('获取班级详情失败')
      }
    }
  }
}
</script>

<style scoped>
.student-class {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

h4 {
  margin: 0 0 15px 0;
  color: #303133;
}

.el-descriptions {
  margin-bottom: 20px;
}
</style>